package com.coalmine.activiti.webadmin.upms.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.coalmine.activiti.webadmin.upms.model.Table;

/**
 * @author Liuxiaoyang
 * Date  2020-07-28
 */
@Mapper
public interface TableMapper {
    /**
     * 查询数据库表list
     * @param tableName
     * @return
     */
    @Select(
            {"<script>" +
                    "select table_name tableName,table_comment tableComment from information_schema.tables where table_name not  like 'act_%'  and table_name not  like 'config_%' " +
                    "and table_name not  like 'gen_%' and table_name not  like 'bpmn_%' and table_name not  like 'sys_%' and table_name not  like 'qrtz_%' " +
                    "and table_name not  like 'his_%' " +
                    "and table_name not  like 'jimu_%' " +
                    "<if test = \"tableName != null and tableName != ''\">" +
                    "and table_name like concat('%',#{tableName},'%') " +
                    "</if> " +
                    " and table_schema=(select database())" +
                    " and table_name like 'mm%'" +
                    "</script>"})
    List<Table> getTable(@Param("tableName") String tableName);

    /**
     * 根据表名和主键查询数据
     * @param tableName
     * @return
     */
    @Select("select * from ${tableName}  where ${idColumnName}=#{instId} ")
    Map getTableByParams(@Param("tableName") String tableName,@Param("idColumnName") String idColumnName, @Param("instId") String instId);

    /**
     * 根据id动态更新表字段
     * @param tableName
     * @param statusColumnName
     * @param idColumnName
     * @param instId
     * @param status
     * @return
     */
    @Update("update ${tableName} set ${statusColumnName}=#{status} where ${idColumnName}=#{instId} ")
    int updateTable(@Param("tableName") String tableName, @Param("statusColumnName") String statusColumnName, @Param("idColumnName") String idColumnName, @Param("instId") String instId, @Param("status") int status);

    /**
     * 根据id更新表提交时间和状态
     * @param tableName
     * @param submitDate
     * @param statusColumnName
     * @param idColumnName
     * @param instId
     * @param status
     * @return
     */
    @Update("update ${tableName} set ${statusColumnName}=#{status}, submit_date=#{submitDate}  where ${idColumnName}=#{instId} ")
    int updateTableSubmitDate(@Param("tableName") String tableName, @Param("submitDate") Date submitDate, @Param("statusColumnName") String statusColumnName, @Param("idColumnName") String idColumnName, @Param("instId") String instId, @Param("status") int status);
}
